﻿using System;
using System.Data;
using System.Text;
using System.Collections;

namespace BizElements.Core
{
    /// <summary>
    /// Provides methods that create typical SET expressions used by UPDATE statement.
    /// </summary>
    public static class UpdateExpressionFactory
    {
        #region Create methods.

        /// <summary>
        /// Creates a new update expression that sets the given value.		
        /// </summary>
        /// <param name="column">Column which is to be changed.</param>
        /// <param name="newValue">New value. May be <b>null</b>.</param>
        /// <returns>Expression.</returns>
        public static UpdateExpression Value(IDbColumn column, object newValue)
        {
            if (newValue != null)
                return new UpdateExpression(column, newValue);
            else
                return UpdateExpressionFactory.Null(column);
        }

        /// <summary>
        /// Creates a new update expression that changes a column to <b>null</b>.
        /// </summary>
        /// <param name="column">Column which is to be changed.</param>
        /// <returns>Expression.</returns>
        public static UpdateExpression Null(IDbColumn column)
        {
            return new UpdateExpression(column, DBNull.Value);
        }

        /// <summary>
        /// Creates a new update expression that copies the value from another column.
        /// </summary>
        /// <param name="targetColumn">Column which is to be changed.</param>
        /// <param name="sourceColumn">Source column that contains the new value. 
        /// May belong to same or other table which is joined through <see cref="UpdateStatement.Where"/> property.</param>
        /// <returns>Expression.</returns>
        public static UpdateExpression OtherColumn(IDbColumn targetColumn, IDbColumn sourceColumn)
        {
            DbColumnCollection sourceCols = new DbColumnCollection();
            sourceCols.Add(sourceColumn);
            return new UpdateExpression(UpdateExpressionType.OtherColumn, targetColumn, sourceCols, sourceColumn, SqlItemType.Column);
        }

        #endregion
    }
}